package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {
    @Autowired
    private UserInfoMapper userInfoMapper;

    //保存用户信息
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    //更新头像
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long userid) {
        UserInfo userInfo = userInfoMapper.selectById(userid);
        return userInfo;
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> ids) {
        if (CollUtil.isEmpty(ids)){
            return null;
        }
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.in("id",ids);
        List<UserInfo> userInfoList = userInfoMapper.selectList(wrapper);
        Map<Long, UserInfo> map = new HashMap<>();
        for (UserInfo userInfo : userInfoList) {
            map.put(userInfo.getId(),userInfo);
        }

        return map;
    }

    //分页查询全部
    public IPage findAll(Integer page, Integer pagesize) {
        Page<UserInfo> infoPage = new Page<>(page, pagesize);
      return userInfoMapper.selectPage(infoPage, null);

    }

    /**
     * 查询用户id和照片
     * @param idList
     * @return
     */
    @Override
    public List<UserInfo> findSameUserInfo(List<Long> idList) {
        List<UserInfo> userInfoList = userInfoMapper.selectBatchIds(idList);
        return userInfoList;
    }

}
